package com.submit.demo.controller;

import com.submit.demo.annotation.AuthIgnore;
import com.submit.demo.common.api.CommonPage;
import com.submit.demo.common.api.CommonResult;
import com.submit.demo.dto.FilterSubmitterParam;
import com.submit.demo.dto.SubmitterReturnData;
import com.submit.demo.dto.TaskInsertParam;
import com.submit.demo.model.Submitter;
import com.submit.demo.model.Task;
import com.submit.demo.repository.SubTaskRepository;
import com.submit.demo.service.SubTaskService;
import com.submit.demo.service.SubmitterService;
import com.submit.demo.service.TaskService;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@RequestMapping("task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @Autowired
    private SubTaskService subTaskService;

    @Autowired
    private SubmitterService submitterService;

    @Autowired
    private SubTaskRepository subTaskRepository;

    /**
     * 新建一个任务
     */
    @AuthIgnore
    @ApiOperation("新建任务")
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public CommonResult<String> createTask(TaskInsertParam taskInsertParam){
        String result = taskService.createTask(taskInsertParam);
        if(!result.equals("")) return CommonResult.success(result,"新建成功");
        return CommonResult.failed("");
    }

    /**
     * 查询收集人的任务列表
     */
    @AuthIgnore
    @ApiOperation("查询收集人的任务列表")
    @ResponseBody
    @RequestMapping(value = "/colTasks", method = RequestMethod.GET)
    public CommonResult<CommonPage<Task>> colTasksList(@RequestParam("cid") String cid, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        CommonPage<Task> list = taskService.selectTasksByCid(cid, pageNum, pageSize);
        return CommonResult.success(list);
    }

    @AuthIgnore
    @ApiOperation("查询已提交人数")
    @ResponseBody
    @RequestMapping(value = "/subCnt", method = RequestMethod.GET)
    public CommonResult<Integer> getSubCnt(@RequestParam("tid") String tid){
        Integer count = subTaskRepository.countByStatusIsAndTidIs(1, tid);
        return CommonResult.success(count);
    }

    @AuthIgnore
    @ApiOperation("查询未提交人数")
    @ResponseBody
    @RequestMapping(value = "/dsubCnt", method = RequestMethod.GET)
    public CommonResult<Integer> getDSubCnt(@RequestParam("tid") String tid){
        Integer count = subTaskRepository.countByStatusIsAndTidIs(0, tid);
        return CommonResult.success(count);
    }


    /**
     * 查询提交人的任务列表
     */
    @AuthIgnore
    @ApiOperation("查询提交人的任务列表")
    @ResponseBody
    @RequestMapping(value = "/subTasks", method = RequestMethod.GET)
    public CommonResult<CommonPage<Task>> subTasksList(@RequestParam("sid") String sid, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        CommonPage<Task> list = taskService.selectTasksBySid(sid, pageNum, pageSize);
        return CommonResult.success(list);
    }

    /**
     * 查询提交人的任务列表
     */
    @AuthIgnore
    @ApiOperation("查询该任务下的所有提交人")
    @ResponseBody
    @RequestMapping(value = "/getListByTid", method = RequestMethod.GET)
    public CommonResult<CommonPage<Submitter>> getListByTid(@RequestParam("tid") String tid, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        CommonPage<Submitter> list = taskService.getListByTid(tid, pageNum, pageSize);
        return CommonResult.success(list);
    }

    /**
     * 收集人启动任务
     */
    @AuthIgnore
    @ApiOperation("收集人启动任务")
    @ResponseBody
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    //@PreAuthorize("hasAnyAuthority('task:start')")
    public CommonResult startTask(String tid){
        int result = taskService.startTask(tid);
        if(result > 0) return CommonResult.success(null, "启动成功");
        return CommonResult.failed();
    }

    /**
     * 收集人暂停任务
     */
    @AuthIgnore
    @ApiOperation("收集人暂停任务")
    @ResponseBody
    @RequestMapping(value = "/pause", method = RequestMethod.GET)
    //@PreAuthorize("hasAnyAuthority('task:pause')")
    public CommonResult pauseTask(String tid){
        int result = taskService.pauseTask(tid);
        if(result > 0) return CommonResult.success(null, "暂停成功");
        return CommonResult.failed();
    }

    /**
     * 查看已提交人列表
     */
    @AuthIgnore
    @ApiOperation("查看已提交人列表")
    @ResponseBody
    @RequestMapping(value = "/slist", method = RequestMethod.GET)
    public CommonResult<CommonPage<SubmitterReturnData>> getSlist(@RequestParam("tid") String tid, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        CommonPage<SubmitterReturnData> list = submitterService.getSlist(tid, pageNum, pageSize);
        return CommonResult.success(list);
    }

    /**
     * 查看未提交人列表
     */
    @AuthIgnore
    @ApiOperation("查看未提交人列表")
    @ResponseBody
    @RequestMapping(value = "/dlist", method = RequestMethod.GET)
    public CommonResult<CommonPage<SubmitterReturnData>> getDlist(@RequestParam("tid") String tid, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum){
        CommonPage<SubmitterReturnData> list = submitterService.getDlist(tid, pageNum, pageSize);
        return CommonResult.success(list);
    }

    @AuthIgnore
    @ApiOperation("获得所有的任务列表")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Task>> getList(@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "-1") Integer status){
        CommonPage<Task> list = taskService.getList(pageNum, pageSize, status);
        return CommonResult.success(list);
    }

    @AuthIgnore
    @ApiOperation("获得单个任务详情")
    @ResponseBody
    @RequestMapping(value = "/getTask", method = RequestMethod.GET)
    public CommonResult<Task> getTask(String id){
        return CommonResult.success(taskService.getTask(id));
    }



    /**
     * 为一个任务指定多个文档提交人
     */
    @AuthIgnore
    @ApiOperation("为一个任务指定多个文档提交人")
    @ResponseBody
    @RequestMapping(value = "/filter", method = RequestMethod.GET)
    public CommonResult filterSubmitters(FilterSubmitterParam param){
        int result = subTaskService.impSubTasks(param.getTid(), param.getSids());
        if(result > 0) return CommonResult.success(null, "指定多个文档提交人成功");
        return CommonResult.failed("为指定提交人或指定提交人失败");
    }

    @AuthIgnore
    @ApiOperation("修改任务名称")
    @ResponseBody
    @RequestMapping(value = "/changeTaskName", method = RequestMethod.GET)
    public CommonResult changeTaskName(String tid, String title){
        Integer result = taskService.changeTaskFileName(tid, title);
        if(result == -1) return CommonResult.failed("修改失败");
        return CommonResult.success(null, "修改成功");
    }

    @AuthIgnore
    @ApiOperation("修改任务截止日期")
    @ResponseBody
    @RequestMapping(value = "/changeDate", method = RequestMethod.GET)
    public CommonResult changeDate(String id, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date){
        Integer result = taskService.changeDate(id, date);
        if(result <= 0) return CommonResult.failed("修改失败");
        return CommonResult.success(null, "修改成功");
    }

    /**
     * 收集人取消任务
     */
    @AuthIgnore
    @ApiOperation("收集人取消任务")
    @ResponseBody
    @RequestMapping(value = "/cancel", method = RequestMethod.GET)
    public CommonResult cancelTask(String tid){
        int result = taskService.cancelTask(tid);
        if(result > 0) return CommonResult.success(null, "取消成功");
        return CommonResult.failed();
    }

    @AuthIgnore
    @ApiOperation("上传任务模板文档")
    @ResponseBody
    @PostMapping("upload")
    public CommonResult upload(@RequestParam("words") MultipartFile[] words,
                               @RequestParam("tid") String tid){
        Assert.notNull(words, "文件不能为空");
        Assert.notNull(tid, "任务id不能为空");
        Integer result = taskService.upload(words, tid);
        if(result > 0) return CommonResult.success(null, "上传成功");
        return CommonResult.failed("上传失败");
    }
}
